package com.berchen.Stream;

import com.berchen.Lambda表达式.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 练习Stream
 */
public class ExerciseStream {

    List<Employee> emps=Arrays.asList(
            new Employee("chen",12,22, Employee.Status.FREE),
            new Employee("chen",12,22, Employee.Status.BUSY),
            new Employee("chen",12,22, Employee.Status.VOCATION),
            new Employee("chenwan",13,24, Employee.Status.FREE),
            new Employee("chenhao",14,25, Employee.Status.BUSY),
            new Employee("chenwanhao",15,12, Employee.Status.VOCATION)
    );
    /**
        给定一个数字列表，如何返回一个由每个数的平方构成的列表。
        给定【1,2,3,4,5】 返回【1,4,9,16,25】
     */
    @Test
    public void test1(){


        Arrays.asList(1,2,3,4,5)
                .stream()
                .map(x->x*x)
                .forEach(System.out::println);
    }

    /**
     * 用map和reduce方法数一数流中有多少个Employee
     */
    @Test
    public void test2() {

        Optional<Integer> reduce = emps.stream()
                .map(x -> 1)
                .reduce(Integer::sum);  // 每一个流都返回一个1  然后reduce将1进行整合
        System.out.println(reduce.get());
    }
}
